package com.iims.flowable.flow;

import java.lang.management.ManagementFactory;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fanpei
 * @date 2021/6/25 11:19
 */
public class DateUtil {
    public static final String DATE_FORMAT_DEFAULT = "yyyy-MM-dd";
    public static final String DATE_FORMAT_SLASH = "yyyy/MM/dd";
    public static final String DATE_FORMAT_NOT_ALL = "yyyyMMdd";
    public static final String DATE_FORMAT_CN = "yyyy'年'MM'月'dd'日'";
    public static final String DATE_FORMAT_MONTH_DAY_YEAR = "M/d/yyyy";
    public static final String DATETIME_FORMAT_DEFAULT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATETIME_FORMAT_SLASH = "yyyy/MM/dd HH:mm:ss";
    public static final String DATETIME_FORMAT_NOT_SLASH = "yyyyMMdd HH:mm:ss";
    public static final String DATETIME_FORMAT_NOT_ALL = "yyyyMMddHHmmss";
    public static final String TIME_FORMAT_DEFAULT = "HH:mm:ss";
    public static final String TIME_FORMAT_NOT_ALL = "HHmmss";
    private static ThreadLocal<Date> threadlocalDate = new ThreadLocal();
    private static final Object LOCK_OBJ = new Object();
    private static Map<String, ThreadLocal<SimpleDateFormat>> simpleDateFormatMap = new HashMap();

    public DateUtil() {
    }

    public static Date getNow() {
        Date date = (Date) threadlocalDate.get();
        if (date == null) {
            date = new Date();
            threadlocalDate.set(date);
        }

        return date;
    }

    public static void clearNow() {
        Date date = (Date) threadlocalDate.get();
        if (date != null) {
            threadlocalDate.remove();
        }

    }

    public static SimpleDateFormat getSimpleDateFormat(final String pattern) {
        ThreadLocal<SimpleDateFormat> threadLocalSimpleDateFormat = (ThreadLocal) simpleDateFormatMap.get(pattern);
        if (threadLocalSimpleDateFormat == null) {
            synchronized (LOCK_OBJ) {
                threadLocalSimpleDateFormat = (ThreadLocal) simpleDateFormatMap.get(pattern);
                if (threadLocalSimpleDateFormat == null) {
                    threadLocalSimpleDateFormat = new ThreadLocal<SimpleDateFormat>() {
                        protected SimpleDateFormat initialValue() {
                            return new SimpleDateFormat(pattern);
                        }
                    };
                    simpleDateFormatMap.put(pattern, threadLocalSimpleDateFormat);
                }
            }
        }

        return (SimpleDateFormat) threadLocalSimpleDateFormat.get();
    }

    public static String dateToStr(Date date, String pattern) {
        if (date == null) {
            return "";
        } else if (pattern == null) {
            return "";
        } else {
            SimpleDateFormat simpleDateFormat = getSimpleDateFormat(pattern);
            return simpleDateFormat.format(date);
        }
    }

    public static String dateToStringYyyymmdd(Date date) {
        return dateToStr(date, "yyyyMMdd");
    }

    public static String dateToStr(Date date) {
        return dateToStr(date, "yyyy-MM-dd");
    }

    public static String dateToStrTime(Date date) {
        return dateToStr(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static int compare(Date firstDate, Date secondDate, String pattern) {
        if (pattern == null) {
            pattern = "yyyy-MM-dd";
        }

        SimpleDateFormat simpleDateFormat = getSimpleDateFormat(pattern);
        firstDate = strToDate(simpleDateFormat.format(firstDate), pattern);
        secondDate = strToDate(simpleDateFormat.format(secondDate), pattern);
        Calendar firstCalendar = null;
        if (firstDate != null) {
            firstCalendar = Calendar.getInstance();
            firstCalendar.setTime(firstDate);
        }

        Calendar secondCalendar = null;
        if (firstDate != null) {
            secondCalendar = Calendar.getInstance();
            secondCalendar.setTime(secondDate);
        }

        try {
            return firstCalendar.compareTo(secondCalendar);
        } catch (NullPointerException var7) {
            throw new IllegalArgumentException(var7);
        } catch (IllegalArgumentException var8) {
            throw new IllegalArgumentException(var8);
        }
    }

    public static String objToStr(Object obj) {
        if (obj.getClass() == String.class) {
            return (String) obj;
        } else if (obj.getClass() == Date.class) {
            return dateToStrTime((Date) obj);
        } else {
            return obj.getClass() == Timestamp.class ? dateToStrTime(new Date(((Timestamp) obj).getTime())) : obj.toString();
        }
    }

    public static Date strToDate(String dateStr, String pattern) {
        try {
            if (dateStr != null && dateStr.length() != 0) {
                if (pattern == null) {
                    pattern = "yyyy-MM-dd";
                }

                SimpleDateFormat simpleDateFormat = getSimpleDateFormat(pattern);
                return simpleDateFormat.parse(dateStr);
            } else {
                return null;
            }
        } catch (Exception var3) {
            return null;
        }
    }

    public static Date strToDateTime(String dateStr) {
        try {
            return strToDate(dateStr, "yyyy-MM-dd HH:mm:ss");
        } catch (Exception var2) {
            return null;
        }
    }

    public static Date add(Date date, int field, int amount) {
        if (date == null) {
            date = new Date();
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(field, amount);
        return cal.getTime();
    }

    public static Date addMilliSecond(Date date, int amount) {
        return add(date, 14, amount);
    }

    public static Date addSecond(Date date, int amount) {
        return add(date, 13, amount);
    }

    public static Date addMiunte(Date date, int amount) {
        return add(date, 12, amount);
    }

    public static Date addHour(Date date, int amount) {
        return add(date, 10, amount);
    }

    public static Date addDay(Date date, int amount) {
        return add(date, 5, amount);
    }

    public static Date addMonth(Date date, int amount) {
        return add(date, 2, amount);
    }

    public static Date addYear(Date date, int amount) {
        return add(date, 1, amount);
    }

    public static int getDateDeff(Date startDate, Date endDate) {
        int intValue = 0;
        String df = "yyyy-MM-dd";
        startDate = strToDate(dateToStr(startDate, df), df);
        endDate = strToDate(dateToStr(endDate, df), df);
        intValue = (int) ((startDate.getTime() - endDate.getTime()) / 86400000L);
        return intValue;
    }

    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 86400000L;
        long nh = 3600000L;
        long nm = 60000L;
        long diff = endDate.getTime() - nowDate.getTime();
        long day = diff / nd;
        long hour = diff % nd / nh;
        long min = diff % nd % nh / nm;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }
}
